﻿#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import cgi
import datetime
import wsgiref.handlers
from google.appengine.api import urlfetch
from google.appengine.ext import webapp

class Getstock(webapp.RequestHandler):
	def get(self):
		code=self.request.get('code')
		if code[:1]=='0':
			url="http://hq.sinajs.cn/list=sz"+code
		else:
			url="http://hq.sinajs.cn/list=sh"+code
		result = urlfetch.fetch(url)
		if result.status_code == 200:
			content=result.content
			usplitdata=[]
			usplitdata=content.split(",")
			self.response.out.write(usplitdata[3]+"|"+usplitdata[2]+"|"+usplitdata[8]+"|"+usplitdata[9])
		else:
			self.response.out.write("0|0|0|0")

application = webapp.WSGIApplication([
  ('/getstock', Getstock)
], debug=True)


def main():
  wsgiref.handlers.CGIHandler().run(application)


if __name__ == '__main__':
  main()
